//
//  MHGiftDetailViewController.m
//  MHO
//
//  Created by Hien NV on 6/20/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHGiftDetailViewController.h"
#import "Application.h"
#import "MHGameDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "Contants.h"
#import "ServiceLib.h"
#import "Lib.h"


@interface MHGiftDetailViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *appIcon;
@property (strong, nonatomic) IBOutlet UILabel *appNameLb;
@property (strong, nonatomic) IBOutlet UILabel *appNameLb2;
@property (strong, nonatomic) NSString* giftCode;
@end

@implementation MHGiftDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self.navibarView leftButton] setImage:[UIImage imageNamed:kFileBtnBack] forState:UIControlStateNormal];
    // Do any additional setup after loading the view from its nib.
    [self.appIcon setImageWithURL:[Contants getImageUrl:self.app.imagePath width:100 height:100]];
    self.appNameLb.text = self.app.appName;
    self.appNameLb2.text = self.app.appName;
    
    [self getGiftCode];
}

- (void)getGiftCode {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString* url = [NSString stringWithFormat:@"http://mobile.mho.vn/services/GiftCode?Email=%@&GameId=%@&key=%@", [Lib stringForKey:@"Email"], self.app.appId, kServerKey];
        NSString* response = [ServiceLib sendGetRequest:url useCache:NO];
        if (response) {
            SBJsonParser* parser = [[SBJsonParser alloc] init];
            id obj = [parser objectWithString:response];
            if (obj && [obj isKindOfClass:[NSDictionary class]]) {
                self.giftCode = [obj objectForKey:@"FileGiftCode"];
            }
        }
    });
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)detailGamePressed:(id)sender {
    MHGameDetailViewController* vc = [[MHGameDetailViewController alloc] initWithNibName:@"MHGameDetailViewController" bundle:nil];
    vc.app = self.app;
    vc.appId = self.app.appId.intValue;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)receiveGift:(id)sender {
    if (self.giftCode) {
        UIPasteboard* paste = [UIPasteboard generalPasteboard];
        [paste setString:self.giftCode];
        [Lib showMessage:[NSString stringWithFormat:@"Gift code của bạn là: %@! Đã copy gift code vào clipboard!", self.giftCode] title:@""];
    }
    else{
        [Lib showMessage:@"Bạn không nhận được gift code nào!" title:@""];
    }
}

@end
